package xdu.lz.stage1_basic.chapter2_api.e_close;

public class ShutDown2 {
    private static class Worker extends Thread{
        private volatile boolean  start = true;
        @Override
        public void run() {
            while (start){
                //方式1：
                if(Thread.interrupted()){
                    break;
                }
                //方式2：
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    break; //return
                }
            }
        }
        public void shutdown(){
            this.start = false;
        }
    }

    public static void main(String[] args) {
        Worker worker = new Worker();
        worker.start();
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        worker.interrupt();
    }

}
